package cn.wym.ymbill

import cn.wym.ymbill.base.BaseResponse
import cn.wym.ymbill.http.EResponseWrapper
import cn.wym.ymbill.model.BillBeen
import cn.wym.ymbill.service.impl.BillService
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import java.util.*
import javax.annotation.Resource
import cn.wym.ymbill.expand.parse
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.RequestBody
import java.lang.Exception


@Controller
@RequestMapping("/ymbill")
class BillController {

    val logger = LoggerFactory.getLogger("BillController")

    @Resource
    val billService: BillService? = null

    @RequestMapping("/index", method = [RequestMethod.GET])
    @ResponseBody
    fun index() : String {
        return "" + billService?.insert(BillBeen(billCode = "0001", billDate = Date(), billAddress = "上海", billUser = "那么大"))
//        return "ni de tou fa dian zhao le"
    }

    @RequestMapping("/testHtml")
    fun testHtml(model: Model) : String {
        model.apply {
            addAttribute("name", "小sss大哈哈哈自动部署")
            addAttribute("website", "PC")
            addAttribute("language", "简体中文")
        }
        return "test"
    }

    @RequestMapping(value = ["/add"], method = [RequestMethod.POST])
    @ResponseBody
    fun add(@RequestBody billBeen: BillBeen?) : BaseResponse<Any?> {
        logger.error("----billBeen: $billBeen")
        return try {
            val num = billBeen?.let { billService?.insertSelective(it) }
            EResponseWrapper.SUCCESS.parse(num)
        } catch (ex: Exception) {
            ex.printStackTrace()
            EResponseWrapper.FAILED.parse(null)
        }
    }

    @RequestMapping(value = ["/remove"], method = [RequestMethod.DELETE])
    @ResponseBody
    fun delete(@RequestBody billCode: Int) {

    }

    fun find() {
        
    }


}
